<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\mycurrency\agent\Agent;
use app\admin\model\mycurrency\Device;
use app\admin\model\mycurrency\lease\Order;
use app\admin\model\mycurrency\merchant\Store;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\mycurrency\LeaseOrder;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        //20250521冯
        //订单统计 3=支付租赁费用中【先付后用】,4=使用中,5=支付租赁费用中【先用后付or补交费用】,6=已完成
        $order_num = Order::where(['use_status'=>['in',[LeaseOrder::USE_STATUS_ZHIFUXIANFU,LeaseOrder::USE_STATUS_JINXINGZHONG,LeaseOrder::USE_STATUS_ZHIFUHOUFU,LeaseOrder::USE_STATUS_YIWANCHENG]]])->count();
        //门店统计
        $store_num = Store::where(['status'=>1])->count();
        //代理数
        $agent_num = Agent::where(['status'=>1])->count();
        //总流水
        $order_fee_total = Order::where(['use_status'=>LeaseOrder::USE_STATUS_YIWANCHENG])->sum('paid_dlready');
        $totay = Date::unixtime();
        $yesterday = Date::unixtime('day',-1);
        $week = Date::unixtime('day',-7);
        $month = Date::unixtime('day',-30);
        $circumTime = $yesterday.','.$totay;
        $order_fee_totay = Order::where(['use_status'=>LeaseOrder::USE_STATUS_YIWANCHENG,'createtime'=>['>',$totay]])->sum('paid_dlready');
        $order_fee_yesterday = Order::where(['use_status'=>LeaseOrder::USE_STATUS_YIWANCHENG,'createtime'=>['between',$circumTime]])->sum('paid_dlready');
        $order_fee_week = Order::where(['use_status'=>LeaseOrder::USE_STATUS_YIWANCHENG,'createtime'=>['>',$week]])->sum('paid_dlready');
        $order_fee_month = Order::where(['use_status'=>LeaseOrder::USE_STATUS_YIWANCHENG,'createtime'=>['>',$month]])->sum('paid_dlready');
        //在线设备
        $device_online_num = Device::where(['is_online'=>\app\common\model\mycurrency\Device::IS_ONLINE_ZAIXIAN])->count();
        //不在线设备
        $device_no_online_num = Device::where(['is_online'=>\app\common\model\mycurrency\Device::IS_ONLINE_BUZAIXIAN])->count();
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            //20250521冯
            'order_num'        => $order_num,
            'store_num'        => $store_num,
            'agent_num'        => $agent_num,
            'order_fee_total'        => $order_fee_total,
            'order_fee_totay'        => $order_fee_totay,
            'order_fee_yesterday'        => $order_fee_yesterday,
            'order_fee_week'        => $order_fee_week,
            'order_fee_month'        => $order_fee_month,
            'device_online_num'        => $device_online_num,
            'device_no_online_num'        => $device_no_online_num,
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }

}
